package com.woniu.smart.service.impl;

import com.github.pagehelper.PageHelper;
import com.woniu.smart.dto.CustomerDTO;
import com.woniu.smart.dto.FollowDTO;
import com.woniu.smart.entity.Customer;
import com.woniu.smart.entity.Follow;
import com.woniu.smart.entity.Needs;
import com.woniu.smart.mapper.CustomerMapper;
import com.woniu.smart.mapper.FollowMapper;
import com.woniu.smart.mapper.NeedsMapper;
import com.woniu.smart.mapper.SysAdminMapper;
import com.woniu.smart.service.CustomerService;
import com.woniu.smart.vo.CustomerVO;
import com.woniu.smart.vo.FollowVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 * @description：TODO
 *
 * @author     ：mmzs
 * @date       ：2022/10/24 20:03
 */
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private NeedsMapper needsMapper;
    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private SysAdminMapper sysAdminMapper;
    @Override
    public List<CustomerVO> selectAll(CustomerDTO customerDTO) {
        PageHelper.startPage(customerDTO.getCurrentPage(),customerDTO.getPageSize());
        return customerMapper.selectAll(customerDTO);
    }

    @Override
    public CustomerVO selectByCustomerId(Long customerId) {
        List<CustomerVO> list = customerMapper.selectByCustomerId(customerId);
        CustomerVO customerVO = list.get(0);
        return customerVO;
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        int i0 = 0;
        int i1 = needsMapper.deleteByPrimaryKey((long) customerMapper.selectByPrimaryKey(id).getNeedsId());
        List<Long> list = followMapper.selectFollowIdByCustomerId(id);
        for (Long l:list) {
            int i2 = followMapper.deleteByPrimaryKey(l);
            i0 +=i2;
        }
        int i = customerMapper.deleteByPrimaryKey(id);
        return i0+i+i1;
    }

    @Override
    public int deleteAll(Long[] ids) {
        int i = 0;
        for (Long id:ids) {
            int i1 = customerMapper.deleteByPrimaryKey(id);
            i+=i1;
        }
        return i;
    }

    @Override
    public int insert(Customer record) {
        return 0;
    }

    @Override
    public int insertSelectiveCustomer(CustomerVO recordVO) {
        Needs needs = new Needs();
        needs.setNeedsMinarea(recordVO.getNeedsMinarea());
        needs.setNeedsMaxarea(recordVO.getNeedsMaxarea());
        needs.setNeedsMinprice(recordVO.getNeedsMinprice());
        needs.setNeedsMaxprice(recordVO.getNeedsMaxprice());
        needs.setNeedsUse(recordVO.getNeedsUse());
        needs.setNeedsHoperoom(recordVO.getNeedsHoperoom());
        int i = needsMapper.insertSelective(needs);
        Customer customer = new Customer();
        customer.setCustomerName(recordVO.getCustomerName());
        customer.setCustomerPhone(recordVO.getCustomerPhone());
        customer.setCustomerType(recordVO.getCustomerType());
        customer.setCustomerDengji("中");
        customer.setNeedsId(needs.getNeedsId());
        customer.setCustomerCommuni("电话");
        int i1 = customerMapper.insertSelective(customer);
        Follow follow = new Follow();
        follow.setFollowBy1(new Date());
        follow.setFollowDatetime(new Date());
        Date date = new Date(new Date().getTime() + 86400000);
        follow.setFollowTimes(date);
        follow.setEmpId(sysAdminMapper.selectByEmpName(recordVO.getEmpName()));
        follow.setCustomerId(customer.getCustomerId());
        follow.setFollowState(recordVO.getFollowState());
        follow.setFollowBy2("电话");
        follow.setFollowContent("客户初次接触，待后续接触");
        int i2 = followMapper.insertSelective(follow);
        return i+i1+i2;
    }

    @Override
    public int insertSelective(Customer record) {
        return 0;
    }

    @Override
    public Customer selectByPrimaryKey(Long id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(Customer record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKeySelectiveAll(CustomerVO recordVO) {
        Customer customer = customerMapper.selectByPrimaryKey((long) recordVO.getCustomerId());
        customer.setCustomerName(recordVO.getCustomerName());
        customer.setCustomerPhone(recordVO.getCustomerPhone());
        customer.setCustomerDengji(recordVO.getCustomerDengji());
        customer.setCustomerCommuni("电话");
        int i = customerMapper.updateByPrimaryKeySelective(customer);
        Needs needs = needsMapper.selectByPrimaryKey((long)customer.getNeedsId());
        needs.setNeedsMinarea(recordVO.getNeedsMinarea());
        needs.setNeedsMaxarea(recordVO.getNeedsMaxarea());
        needs.setNeedsMinprice(recordVO.getNeedsMinprice());
        needs.setNeedsMaxprice(recordVO.getNeedsMaxprice());
        needs.setNeedsUse(recordVO.getNeedsUse());
        needs.setNeedsHoperoom(recordVO.getNeedsHoperoom());
        int i1 = needsMapper.updateByPrimaryKeySelective(needs);
        Follow follow = new Follow();
        follow.setFollowDatetime(new Date());
        Date date = new Date(new Date().getTime() + 86400000);
        follow.setFollowTimes(date);
        follow.setEmpId(sysAdminMapper.selectByEmpName(recordVO.getEmpName()));
        follow.setCustomerId(customer.getCustomerId());
        follow.setFollowState(recordVO.getFollowState());
        follow.setFollowBy2(recordVO.getCustomerCommuni());
        follow.setFollowBy2("面谈");
        follow.setFollowContent("客户信息详情更改");
        int i2 = followMapper.insertSelective(follow);
        return i+i1+i2;
    }

    @Override
    public int updateByPrimaryKey(Customer record) {
        return 0;
    }
}
